package com.project.decoration.controller.rotation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationRotation;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationRotationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 轮播图controller
 * @date 2025/02/14 10:54
 */
@Controller
@ResponseBody
@RequestMapping("rotation")
public class DecorationRotationController {

    @Autowired
    private DecorationRotationService decorationRotationService;

    /** 分页获取轮播图 */
    @PostMapping("getDecorationRotationPage")
    public Result getDecorationRotationPage(@RequestBody DecorationRotation decorationRotation) {
        Page<DecorationRotation> page = new Page<>(decorationRotation.getPageNumber(),decorationRotation.getPageSize());
        QueryWrapper<DecorationRotation> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(decorationRotation.getImage()),DecorationRotation::getImage,decorationRotation.getImage());
        Page<DecorationRotation> decorationRotationPage = decorationRotationService.page(page, queryWrapper);
        return Result.success(decorationRotationPage);
    }

    @GetMapping("getDecorationRotationList")
    public Result getDecorationRotationList() {
        List<DecorationRotation> rotationList = decorationRotationService.list();
        return Result.success(rotationList);
    }

    /** 根据id获取轮播图 */
    @GetMapping("getDecorationRotationById")
    public Result getDecorationRotationById(@RequestParam("id")String id) {
        DecorationRotation decorationRotation = decorationRotationService.getById(id);
        return Result.success(decorationRotation);
    }

    /** 保存轮播图 */
    @PostMapping("saveDecorationRotation")
    public Result saveDecorationRotation(@RequestBody DecorationRotation decorationRotation) {
        boolean save = decorationRotationService.save(decorationRotation);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑轮播图 */
    @PostMapping("editDecorationRotation")
    public Result editDecorationRotation(@RequestBody DecorationRotation decorationRotation) {
        boolean save = decorationRotationService.updateById(decorationRotation);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除轮播图 */
    @GetMapping("removeDecorationRotation")
    public Result removeDecorationRotation(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationRotationService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("轮播图id不能为空！");
        }
    }

}
